package com.dudu.service.impl;

import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.dudu.bean.CommunicationDataQuerying;
import com.dudu.dao.AccountExtendDao;
import com.dudu.dao.CommunicationDataDao;
import com.dudu.model.AccountExtend;
import com.dudu.utils.Encrypt;

import core.CoreDao;
import core.CoreDaoInvoke;


/**
 * 
 * @author jack
 *
 */
@Service
public class CommunicationDataImpl implements com.dudu.service.ICommunicationDataService {


	private CoreDao coreDao = CoreDaoInvoke.getInstance();
	private CommunicationDataDao communicationDataDao = new CommunicationDataDao();
	private AccountExtendDao accountExtendDao = new AccountExtendDao();
	

	@Override
	public List<JSONObject> queryCommunicationDataQueryingList(CommunicationDataQuerying bean, String serialNumber) {
		List<JSONObject> list = communicationDataDao.queryCommunicationDataQueryingList(coreDao, bean);
		for(JSONObject obj:list){
			CommunicationDataQuerying entity = new CommunicationDataQuerying();
			entity.setId(obj.optInt("id"));
			entity.setSerial_number(serialNumber);
			communicationDataDao.updateCommunicationDataQueryingById(coreDao, entity);
			obj.remove("id");
		}
		return list;
	}

	@Override
	public int deleteCommunicationDataQueryingBySerialNumber(String serialNumber) {
		communicationDataDao.insertCommunicationDataQueryedBySerialNumber(coreDao, serialNumber); //转存至已查询表
		return communicationDataDao.deleteCommunicationDataQueryingBySerialNumber(coreDao, serialNumber); //从待查询表中删除
	}

	@Override
	public boolean authCustAccount(String custAccount, String timestamp, String sign) {
		boolean result = false;
		if(StringUtils.isNotBlank(custAccount)&&StringUtils.isNotBlank(timestamp)&& StringUtils.isNotBlank(sign)){
			AccountExtend accountEntity = accountExtendDao.queryAccountExtendByCustAccount(coreDao, custAccount);
			if(accountEntity != null){
				String signResult =Encrypt.sha1Encrypt(custAccount + accountEntity.getKey_private() + timestamp);
				if (sign.equalsIgnoreCase(signResult)) {
					result = true;
				}
			}
		}
		return result;
	}

}
